package controller

import (
	"github.com/gin-gonic/gin"
	"shop-bean/constant"
	"shop-bean/request"
	"shop-common/library/serializer"
	"shop-common/utils"
	"shop-common/utils/pagehelper"
	"shop-service/business"
)

type SystemStoreController struct {
}

func (*SystemStoreController) Save(ctx *gin.Context) {
	form := request.SystemStoreRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if err := business.ServiceImpl().SystemStore.Create(&form); err != nil {
		serializer.Fail(ctx, serializer.PublicCreateErr)
		return
	}

	serializer.Success(ctx, "")
}

func (*SystemStoreController) Page(ctx *gin.Context) {
	form := pagehelper.SearchPageRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if has := utils.IntIncludes([]int{constant.GLOBAL_DISABLE, constant.GLOBAL_ENABLE, constant.GLOBAL_LOGIC_RECYCLE}, form.Status); !has {
		serializer.Fail(ctx, "查询类型不合法")
		return
	}

	page, err := business.ServiceImpl().SystemStore.FindPage(&form)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", page)
}

func (*SystemStoreController) Count(ctx *gin.Context) {
	headerNum, err := business.ServiceImpl().SystemStore.FindStatistic()
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", &headerNum)
}

func (*SystemStoreController) Update(ctx *gin.Context) {
	form := request.SystemStoreRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if form.Id <= 0 {
		serializer.Fail(ctx, "id不能为空")
		return
	}

	if err := business.ServiceImpl().SystemStore.Updates(&form); err != nil {
		serializer.Fail(ctx, serializer.PublicUpdateErr)
		return
	}
	serializer.Success(ctx, "")
}

func (*SystemStoreController) UpdateStatus(ctx *gin.Context) {
	form := request.UpdateSystemStoreRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if err := business.ServiceImpl().SystemStore.UpdateStatus([]uint{form.Id}, form.Status); err != nil {
		serializer.Fail(ctx, serializer.PublicUpdateErr)
		return
	}

	serializer.Success(ctx, "")
}

func (*SystemStoreController) Recovery(ctx *gin.Context) {
	form := request.CommonBatchRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if err := business.ServiceImpl().SystemStore.UpdateStatus(form.Ids, constant.GLOBAL_ENABLE); err != nil {
		serializer.Fail(ctx, serializer.PublicUpdateErr)
		return
	}

	serializer.Success(ctx, "")
}

func (*SystemStoreController) Delete(ctx *gin.Context) {
	form := request.CommonBatchRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if err := business.ServiceImpl().SystemStore.UpdateStatus(form.Ids, constant.GLOBAL_LOGIC_RECYCLE); err != nil {
		serializer.Fail(ctx, serializer.PublicDeleteErr)
		return
	}

	serializer.Success(ctx, "")
}

func (*SystemStoreController) CompleteLyDelete(ctx *gin.Context) {
	form := request.CommonBatchRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if err := business.ServiceImpl().SystemStore.Delete(form.Ids); err != nil {
		serializer.Fail(ctx, serializer.PublicDeleteErr)
		return
	}

	serializer.Success(ctx, "")
}
